import { Message, Modal } from '@arco-design/web-vue'

// 确认操作
export const useConfirm = async (
  operate: () => Promise<any>,
  successCallback?: () => void,
  options?: {
    title?: string
    content?: string
    successTip?: string
    showModal?: boolean
  },
): Promise<boolean | undefined> => {
  const onConfirm = async () => {
    try {
      await operate()
      Message.success(options?.successTip || '删除成功')
      successCallback?.()
      return true
    } catch (error) {
      return false
    }
  }
  // 是否显示对话框
  const flag = options?.showModal ?? true
  if (!flag) {
    return onConfirm()
  }
  Modal.warning({
    title: options?.title || '提示',
    content: options?.content || '是否确定删除该条数据？',
    okButtonProps: { status: 'danger' },
    hideCancel: false,
    maskClosable: true,
    onBeforeOk: onConfirm,
  })
}
